package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.bean.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;



//    跳转到首页
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books= bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }

    //跳转到toAddBook页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(Model model){
        return "addBook";
    }
    //进行添加操作
    @RequestMapping("/addBook")
    public String addBooks(Books books)
    {
        bookService.addBook(books);
        return "redirect:/book/allBooks";
    }

    //跳转到更新页面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(Integer id,Model model){
        Books book = bookService.queryBookById(id);
        model.addAttribute("book",book);
        return "updateBook";
    }

    //进行更新操作
    @RequestMapping("/updateBook")
    public String updateBooks(Books books)
    {
        bookService.updateBook(books);
        return "redirect:/book/allBooks";
    }

    //删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String delete(@PathVariable("bookId") Integer id)
    {
        bookService.deleteBookById(id);
        return "redirect:/book/allBooks";
    }
    //查询书籍包含分页

    @RequestMapping("/allBooks")
    public String getBooks(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model){
        //引入pageHelper分页插件
        //传入页码和每页的大小
        PageHelper.startPage(pn,5);
        List<Books> list = bookService.queryAllBook();
        //传入分页参数
        // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
        // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
        PageInfo pageInfo=new PageInfo(list,3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("list",list);
        return "allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBooks(String queryBookName, Model model,@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        List<Books> books = bookService.queryBookByName(queryBookName);
        if (books==null){
            return "redirect:/book/allBooks";
        }
        PageHelper.startPage(pn,5);
        PageInfo pageInfo=new PageInfo(books,3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("list",books);
        return "allBook";
    }
}
